using Application.Interface;
using Domain.Dto;
using Domain.Interface;
using Domain.Model;
using Domain.Specification;
using Gmmy.ApplicationExtension;
using Gmmy.DomainExtension;
using Gmmy.RepositoryExtension;
using Gmmy.Utility;
using Microsoft.Extensions.Logging;
using Surging.Core.CPlatform.Ioc;
using System.Threading.Tasks;

namespace Application.Services
{
    [ModuleName("Module")]
    public class ModuleService : BaseServices<Module>, IModuleService
    {
        private readonly IModuleDomainService _domainService;
        private readonly IModuleRepository _repository;
        private readonly ILogger<ModuleService> _logger;
        public ModuleService(IModuleRepository repository, IModuleDomainService domainService
		,ILogger<ModuleService> logger
		)
            : base(repository, logger)
        {
			_domainService = domainService;
            _repository = repository;
			_logger = logger;
        }

		/// <summary>
        /// 获取功能模块
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Get(GetInput input)
        {
            return await Do(input, async result =>
            {
                result.data = await _repository.GetModule(input.ID);
            });
        }

        /// <summary>
        /// 获取功能模块列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> GetList(ModuleListInput input)
        {
            return await Do(input, async result =>
            {
                result.data = await _repository.GetList(input,new ModuleListSpecification(input));
            });
        }

        /// <summary>
        /// 创建功能模块
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Create(ModuleCreateInput input)
        {
            return await Do(input, async result =>
            {
                await _domainService.Create(input);
                await base.CommitAsync();
            });
        }

        /// <summary>
        /// 更新功能模块
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Update(ModuleUpdateInput input)
        {
            return await Do(input, async result =>
            {
                await _domainService.Update(input);
                await base.CommitAsync();
            });
        }

        /// <summary>
        /// 逻辑删除功能模块
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> LogicDelete(LogicDeleteInput input)
        {
            return await Do(input, async result =>
            {
                _repository.LogicDelete(new DirectSpecification<Module>(x => input.DeletedIds.Contains(x.ID)));
                await base.CommitAsync();
            });
        }

        /// <summary>
        /// 获取所有功能模块用于角色配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> GetModuleAll(EmptyRequest input)
        {
            return await Do(input, async result =>
            {
                result.data = await _repository.GetModuleAll();
            });
        }

        /// <summary>
        /// 获取所有模块地址
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> GetModuleAddressAll(EmptyRequest input)
        {
            return await Do(input, async result =>
            {
                result.data = await _repository.GetModuleAddressAll();
            });
        }
    }
}